package com.grizz.snake.controller;

import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;

public class InputController {
	private int lastDirectionKeyPressed = -1;
	private Mediator mediator;
	
	public InputController(Mediator mediator) {
		this.mediator = mediator;
		mediator.register(this);
	}
	
	public void update(Input input) throws SlickException{
		if(input.isKeyPressed(Input.KEY_LEFT)) lastDirectionKeyPressed = Input.KEY_LEFT;
		if(input.isKeyPressed(Input.KEY_RIGHT)) lastDirectionKeyPressed = Input.KEY_RIGHT;
		if(input.isKeyPressed(Input.KEY_UP)) lastDirectionKeyPressed = Input.KEY_UP;
		if(input.isKeyPressed(Input.KEY_DOWN)) lastDirectionKeyPressed = Input.KEY_DOWN;
		
		if(input.isKeyPressed(Input.KEY_P) || input.isKeyPressed(Input.KEY_PAUSE)) mediator.setPause(!mediator.isPaused());
		if(input.isKeyPressed(Input.KEY_ESCAPE)) System.exit(0);
		
		if(input.isKeyPressed(Input.KEY_G)) mediator.feedSnake();
		if(input.isKeyPressed(Input.KEY_R)) mediator.resetGameState();
	}
	
	public void reset(){
		lastDirectionKeyPressed = -1;
	}
	
	public int getLastDirectionKeyPressed(){
		return lastDirectionKeyPressed;
	}
}
